$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'mercharReport/storestockmaterial/list',
        datatype: "json",
        colModel: [
			{ label: '仓库名称', name: 'stockName', index: 'stock_name', width: 80 },
            { label: '门店', name: 'sellerName', index: 'seller_name', width: 80 },
            { label: '耗材编号', name: 'materialCode', index: 'material_code', width: 80 },
            { label: '耗材分类名称', name: 'categoryName', index: 'category_name', width: 80 },
            { label: '耗材名称', name: 'materialName', index: 'material_name', width: 80 },
            { label: '耗材库存', name: 'materialQuantity', index: 'material_quantity', width: 80 },
			{ label: '型号', name: 'materialModel', index: 'material_model', width: 80 },
            { label: '规格', name: 'specifications', index: 'specifications', width: 80 },
            { label: '长(单位米)', name: 'length', index: 'length', width: 80 },
            { label: '宽(单位米)', name: 'width', index: 'width', width: 80 },
            { label: '克重(g)', name: 'weight', index: 'weight', width: 80 },
            { label: '成本', name: 'cost', index: 'cost', width: 80 },
			{ label: '库存金额', name: 'money', index: 'money', width: 80 }, 			
			{ label: '单位', name: 'unit', index: 'unit', width: 80 }, 			
			{ label: '备注', name: 'remark', index: 'remark', width: 80 },
            { label: '更新人', name: 'updateName', index: 'update_name', width: 80 },
            { label: '更新时间', name: 'updateTime', index: 'update_time', width: 80 },
        ],
		viewrecords: true,
        height: window.innerHeight-125,
        rowNum: 25,
        rowList: [25, 50, 80],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		storeStockMaterial: {},
		q:{
            materialName:'',
            materialCode:'',
            minQuantity:'',
            maxQuantity:''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.storeStockMaterial = {};
		},
		update: function (event) {
			var stockMaterialId = getSelectedRow();
			if(stockMaterialId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(stockMaterialId)
		},
		saveOrUpdate: function (event) {
			var url = vm.storeStockMaterial.stockMaterialId == null ? "mercharReport/storestockmaterial/save" : "mercharReport/storestockmaterial/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.storeStockMaterial),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var stockMaterialIds = getSelectedRows();
			if(stockMaterialIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "mercharReport/storestockmaterial/delete",
                    contentType: "application/json",
				    data: JSON.stringify(stockMaterialIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(stockMaterialId){
			$.get(baseURL + "mercharReport/storestockmaterial/info/"+stockMaterialId, function(r){
                vm.storeStockMaterial = r.storeStockMaterial;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData: {//postData此数组内容直接赋值到url上，参数类型：{name1:value1…}
                    'materialName': vm.q.materialName,
                    'materialCode':vm.q.materialCode,
                    'minQuantity':vm.q.minQuantity,
                    'maxQuantity':vm.q.maxQuantity,
                },
                page:page
            }).trigger("reloadGrid");
		},
		reset:function () {
			vm.q.materialName='';
		 	vm.q.materialCode='',
			vm.q.minQuantity='',
			vm.q.maxQuantity=''
			vm.reload();
        },
        exportExcel:function () {
            //导出excel
            const data = encodeURIComponent(JSON.stringify(vm.q));
            window.location.href = baseURL + "mercharReport/storestockmaterial/exportExcel?postData=" + data;
        }
    }
});